﻿using System;
using System.Collections.Generic;
using System.Text;
using Tauren.Business.Entity.Model;
using Tauren.Business.Infrastructure.Globals;
using Dapper;
using Dapper.Contrib.Extensions;
using Tauren.Business.Entity.Enum;
using System.Linq;

namespace Tauren.Business.DLL.WorkBenchDLL
{
    public class WorkBenchBusiness : Base.BaseDLL, IWorkBenchBusiness
    {
        public WorkBenchBusiness(IConnectionDLLBase _dLLBase) : base(_dLLBase)
        {
        }

        public bool AddFlowWorkBenches(List<WorkBench> workBenches)
        {
            this.dbConnection.Execute("delete from workbench   where  empid =@EmpId", new { EmpId = Global.EmpId });
            return this.dbConnection.Insert(workBenches) >= 0;
        }

        public List<WorkBench> GetFlowWorkBenches() => this.dbConnection.Query<WorkBench>("select * from workbench w where w.empid =@EmpId", new { EmpId = Global.EmpId }).AsList();

        public List<WorkFlowInfo> GetWorkFlowInfos(long[] flowids = null)
        {
            StringBuilder sb = new StringBuilder($"select f.flowid,fv.versionid  FlowVersionId,fv.formid,fv.formversionid FormVersionId,f.cnname ,f.enname ,f.flowicon ,f.flowstyle ,f.placeholder,f.sequence from flowinfo f inner join flow_version fv on fv.versionno=f.currentversion and fv.flowid =f.flowid where   f.status ={(int)FlowStatus.installed}");
            if (flowids != null && flowids.Any())
            {
                sb.Append($" and f.flowid in({string.Join(',', flowids)})");
            }
            return this.FlowConnection.Query<WorkFlowInfo>(sb.ToString()).AsList();
        }
    }
}
